package com.jack.leetcode.每日一题;

import java.util.ArrayList;
import java.util.List;

/***
 * @author zhangjie
 * @date 2020/3/30 20:50
 */
public class 圆圈中最后剩下的数字 {
    public static void main(String[] args) {
        System.out.println(lastRemaining(4,2));
        System.out.println(lastRemaining(5,3));
        System.out.println(lastRemaining(10,17));
    }

    public static int lastRemaining(int n, int m) {
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<n;i++){
            list.add(i);
        }
        int index = 0;
        while(n > 1){
            index = (index + m - 1) % n;
            list.remove(index);
            n--;
        }
        return list.get(0);
    }
}
